package com.playcrab.adouban.service;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import com.playcrab.adouban.util.Constants;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

public class BootReceiver extends BroadcastReceiver {

	private static final String LOG_TAG = "BootReceiver";


    public static final int BOOT_RECEIVER_WAKE_LOCK_TIMEOUT = 60000;
    
    public static String WAKE_LOCK_RELEASE = "com.fsck.k9.service.BootReceiver.wakeLockRelease";

    public static String WAKE_LOCK_ID = "com.fsck.k9.service.BootReceiver.wakeLockId";

    private static ConcurrentHashMap<Integer, WakeLock> wakeLocks = new ConcurrentHashMap<Integer, WakeLock>();
    private static AtomicInteger wakeLockSeq = new AtomicInteger(0);
 
	private static Integer getWakeLock(Context context)
    {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9");
        wakeLock.setReferenceCounted(false);
        wakeLock.acquire(BOOT_RECEIVER_WAKE_LOCK_TIMEOUT);
        Integer tmpWakeLockId = wakeLockSeq.getAndIncrement();
        wakeLocks.put(tmpWakeLockId, wakeLock);
        if (Constants.DEBUG)
            Log.v(LOG_TAG, "BootReceiver Created wakeLock " + tmpWakeLockId);
        return tmpWakeLockId;
    }

    private static void releaseWakeLock(Integer wakeLockId)
    {
        if (wakeLockId != null)
        {
            WakeLock wl = wakeLocks.remove(wakeLockId);
            if (wl != null)
            {
                if (Constants.DEBUG)
                    Log.v(LOG_TAG, "BootReceiver Releasing wakeLock " + wakeLockId);
                wl.release();
            }
            else
            {
                Log.w(LOG_TAG, "BootReceiver WakeLock " + wakeLockId + " doesn't exist");
            }
        }
    }

    public void onReceive(Context context, Intent intent)
    {
    	Log.i(LOG_TAG,"start onReceive");
        Integer tmpWakeLockId = BootReceiver.getWakeLock(context);
        try
        {
            if (Constants.DEBUG)
                Log.i(LOG_TAG, "BootReceiver.onReceive" + intent);
            if (BootReceiver.WAKE_LOCK_RELEASE.equals(intent.getAction()))
            {
                Integer wakeLockId = intent.getIntExtra(WAKE_LOCK_ID, -1);
                if (wakeLockId != -1)
                {
                    if (Constants.DEBUG)
                        Log.v(LOG_TAG, "BootReceiver Release wakeLock " + wakeLockId);
                    BootReceiver.releaseWakeLock(wakeLockId);
                }
            }
            else
            {
                tmpWakeLockId = receive(context, intent, tmpWakeLockId);
            }
        }
        finally
        {
            BootReceiver.releaseWakeLock(tmpWakeLockId);
        }
    }

    public Integer receive(Context context, Intent intent, Integer wakeLockId)
    {
        return wakeLockId;
    }

    public static void releaseWakeLock(Context context, int wakeLockId)
    {
        if (Constants.DEBUG)
            Log.v(LOG_TAG, "BootReceiver Got request to release wakeLock " + wakeLockId);
        Intent i = new Intent();
        i.setClass(context, BootReceiver.class);
        i.setAction(WAKE_LOCK_RELEASE);
        i.putExtra(WAKE_LOCK_ID, wakeLockId);
        context.sendBroadcast(i);
    }

}
